﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using MToolStudio.Render;
using Microsoft.Mtool.FSM;

namespace MToolStudio.UI
{
    public class AddActionTool : UITool
    {
        //private ActionType actionType = ActionType.Control;
        private StateR from;
        private Point curPos;
        private const int addActionRenderPenWidth = 2;
        private static readonly Color addActionRenderColor = Color.DimGray;

        public AddActionTool(FSMDesigner designer)
            : base(designer)
        {
        }

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            e = TransformToLocal(e);
            StateR tostate = null;
            foreach (IRenderable ir in Designer.RenderItems)
            {
                StateR sr = ir as StateR;
                if (sr == null)
                    continue;

                if (ir.IsSelected(new Point(e.X, e.Y)))
                {
                    if (from == null)
                    {
                        //choose this one
                        from = sr;
                        return;
                    }
                    else
                    {
                        tostate = sr;
                        break;
                    }
                }
            }
            if (tostate == null)
                return;
            Designer.AddAction(new Point(e.X, e.Y), from, tostate);
            from = null;
            Designer.Refresh();
        }

        public override void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (from != null)
            {
                e = TransformToLocal(e);
                curPos = new Point(e.X, e.Y);
                Designer.Refresh();
            }
        }

        public override void Render(Graphics g)
        {
            if (from != null)
            {
                using (Pen pp = new Pen(addActionRenderColor))
                {
                    pp.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDot;
                    g.DrawLine(pp, from.Position, curPos);
                }
            }
        }
    }

}